﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;

namespace projetSupinfoBFG.Base.Model
{
    class ShipTestSprite : Sprite
    {

        protected float rotation;

        public ShipTestSprite(Vector2 position)
            : base(position) 
        {
            this.rotation = 0;
        }

         public ShipTestSprite(float x, float y)
            : base(x, y) 
        {
            this.rotation = 0;
        }

         public float Rotation
         {
             get { return rotation; }
             set { rotation = value; }
         }


        /// <summary>
        /// Update version click continu
        /// </summary>
         /*public void Update()
         {
             MouseState currentMouseState = Mouse.GetState();
             if (currentMouseState.LeftButton == ButtonState.Pressed) 
             {

                Vector2 positionMouse = new Vector2(currentMouseState.X, currentMouseState.Y);
                Vector2 distance = positionMouse - position;
                distance.Normalize();

                //rotation = -(float)Math.Atan2(distance.X, distance.Y) + (float)Math.PI/2;
                 float radius =  -(float)Math.Atan2(distance.X, distance.Y) + (float)Math.PI/2;
                 if (radius > 0)
                 {
                     rotation += 0.4f * (float)Math.PI;
                 }
                 else
                 {
                     rotation -= 0.4f * (float)Math.PI;
                 }

                rotation = -(float)Math.Atan2(distance.X, distance.Y) + (float)Math.PI / 2;

                position += distance * 0.4f;

            }
         }*/

         float dist = 0;
         Vector2 dist_pt = Vector2.Zero;

        /// <summary>
        /// Autopilote version
        /// </summary>
         public void Update()
         {
             if ( dist != 0)
             {
                 if (dist < 0)
                 {
                     dist = 0;
                 }
                 else
                 {
                     dist -= Vector2.Distance(dist_pt, Vector2.Zero) * 0.4f;
                     position += dist_pt * 0.4f;
                 }
                 
             }
             else
             {
                 MouseState currentMouseState = Mouse.GetState();
                 if (currentMouseState.LeftButton == ButtonState.Pressed)
                 {

                     Vector2 positionMouse = new Vector2(currentMouseState.X, currentMouseState.Y);
                     Vector2 distance = positionMouse - position;
                     distance.Normalize();

                     rotation = -(float)Math.Atan2(distance.X, distance.Y) + (float)Math.PI / 2;

                     

                     dist = Vector2.Distance(positionMouse, position);
                     dist_pt = distance;

                     position += distance * 0.4f;
                 }

             }

             


         }
        
        
        
        public override void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, position, null, Color.White, rotation, new Vector2(texture.Height/2, texture.Width/2), 1.0f, SpriteEffects.None, 0);
        }

        

           
    }
}
